using Dou.Scripts.Core.Helpers;
using UnityEngine;

namespace abilities.triggers
{
    public abstract class AbilityTrigger<T>:AbilityTrigger where T : AbilityTriggerTemplate
    {
        
        protected override void Init(AbilityTriggerTemplate prototype, int level)
        {
            this.template = prototype as T;
            Init(this.template, level);
        }
        
        protected virtual void Init(T tpl, int level)
        {
        }
        protected T template;
    }


    //引起效果的东西。触发器必须得计时器更新。例如，被动“hp低于50”，
    public abstract class AbilityTrigger
    {
        protected AbilityContext context;
        protected AbilityTriggerTemplate prototype;
        //***************

        //触发器最后一次激活时间
        long timestamp;

        //应用计数器
        int count;
        int countBeforeActivateCurrent;
        //***************

        public void InitInernal(AbilityTriggerTemplate prototype, int level)
        {
            this.prototype = prototype;
            Init(prototype,level);
        }
        
        protected virtual void Init(AbilityTriggerTemplate prototype, int level)
        {
            this.prototype = prototype;
        }

        // 检查触发器的执行条件
        private bool CheckConditions()
        {
            foreach (var it in prototype.conditions)
            {
                if (!it.Check(context))
                    return false;
            }

            return true;
        }

        public virtual void Start(AbilityContext context)
        {
            this.context = context;
            timestamp = 0;
            countBeforeActivateCurrent = 0;
            Start();
        }

        protected bool OnPerform()
        {
            Debug.Log("onPerform "+ this);

            long now = TimeHelper.Timestamp;
            if (timestamp > 0)
            {
                context.effect.DecrementCooldown(now - timestamp);
            }

            timestamp = now;

            if (prototype.repeat == 0)
            {
                Stop();
                return false;
            }

            // 检查
            if (context.effect.CooldownLeft > 0)
                return false;

            // 检查触发条件
            if (!CheckConditions())
                return false;

            countBeforeActivateCurrent++;
            if (countBeforeActivateCurrent <= prototype.countBeforeActivate)
            {
                return false;
            }

            if (prototype.startTrigger)
            {
                //如果启动计时器有效
                context.effect.ApplyEffect(context.AbilityManager.Owner);

                context.effect.ResetCooldown();
            }
            else
            {
                // 如果计时器停止，效果消失。
                context.AbilityManager.Cancel(context.effect);
            }

            countBeforeActivateCurrent = 0;
            count++;
            if (prototype.repeat > 0 && prototype.repeat <= count)
                Stop();

            return true;
        }

        public AbilityTriggerType TriggerType => prototype.type;

        public abstract void Start();

        public abstract void Stop();

        public virtual void Update(float delta)
        {
        }

        public virtual bool ActivateTrigger(AbilityTriggerType triggerType)
        {
            return false;
        }

        public static implicit operator bool(AbilityTrigger self)
        {
            return self != null;
        }

    }
}